package com.cty.service.impl;

import com.cty.mapper.CustomerMapper;
import com.cty.mapper.RecommendedDishesMapper;
import com.cty.mapper.ShopMapper;
import com.cty.mapper.ShopReviewMapper;
import com.cty.pojo.*;
import com.cty.service.ShopService;
import com.cty.util.ServerResult;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ShopServiceImpl implements ShopService {
    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private RecommendedDishesMapper dishesMapper;
    @Autowired
    private ShopReviewMapper reviewMapper;
    @Autowired
    private CustomerMapper customerMapper;

    @Override
    public ServerResult getByKeyword(String keyword,Integer pageNum) {
        //1. like search : select * from shop where shop_name like %?% or shop_label like %?%
        ShopExample shopExample = new ShopExample();
        shopExample.or().andShopNameLike("%"+keyword +"%");
        shopExample.or().andShopLabelLike("%"+keyword+"%");
        shopExample.setOrderByClause("shop_score desc"); // order by shop_score desc

        //2. select some shops from table shop
        Page<Shop> page = PageHelper.startPage(pageNum,3);
        List<Shop> shopList = shopMapper.selectByExample(shopExample);
        PageInfo pageInfo = page.toPageInfo();//page info is right

        if(shopList!=null && shopList.size()>0){
            List<ShopDetailVO> voList  = new ArrayList<>();

            //3. select compatibale dishes from table dish
            for(Shop shop :shopList){
                System.out.println("service shop :" + shop);
                //4. then return data wth shop and dish VO
                ShopDetailVO  vo = new ShopDetailVO();

                RecommendedDishesExample dishesExample = new RecommendedDishesExample();
                dishesExample.createCriteria().andShopIdEqualTo(shop.getShopId());
                List<RecommendedDishes> dishesList = dishesMapper.selectByExample(dishesExample);
                System.out.println("service dishesList :" + dishesList);
                vo.setShop(shop);
                vo.setRecommendedDishesList(dishesList);
                voList.add(vo);
            }

            pageInfo.setList(voList); // reset pageInfo data , dont update page info
            System.out.println("total:" + pageInfo.getTotal());
            System.out.println("pages:" + pageInfo.getPages());

            System.out.println("service pageinfo :" + pageInfo);
            return ServerResult.getSuccess(pageInfo);
        }else{
            return ServerResult.getFail("没有匹配的店铺");
        }

    }


    // shop     + list<Dish> + Map<Review,Customer>
    @Override
    public ServerResult getById(Integer shopId) {
        ShopDetailVO  vo = new ShopDetailVO();

        Shop shop = shopMapper.selectByPrimaryKey(shopId);

        //select * from dish where shop_id = ?
        RecommendedDishesExample dishesExample = new RecommendedDishesExample();
        dishesExample.createCriteria().andShopIdEqualTo(shop.getShopId());
        List<RecommendedDishes> dishesList = dishesMapper.selectByExample(dishesExample);
        System.out.println("service dishesList :" + dishesList);

        //select * from review where shop_id = ?
        ShopReviewExample reviewExample = new ShopReviewExample();
        reviewExample.createCriteria().andShopIdEqualTo(shop.getShopId());
        reviewExample.setOrderByClause("review_time desc");
        List<ShopReview> reviewList = reviewMapper.selectByExample(reviewExample);
        Map<ShopReview,Customer> reviewCustomerMap = new HashMap<>();

        if(reviewList!=null && reviewList.size()>0){
            for(ShopReview review:reviewList){
                //select * from customer where custmer_id = ?
                Customer customer = customerMapper.selectByPrimaryKey(review.getCusId());
                reviewCustomerMap.put(review,customer);
            }
        }

        vo.setShop(shop);
        vo.setRecommendedDishesList(dishesList);
        vo.setShopReviewCustomerMap(reviewCustomerMap);

        return ServerResult.getSuccess(vo);
    }

    @Override
    public ServerResult getByIdForAddReiew(Integer shopId) {
        ShopDetailVO  vo = new ShopDetailVO();
        //select * from shop where shop_id = ?
        Shop shop = shopMapper.selectByPrimaryKey(shopId);

        //select * from dish where shop_id = ?
        RecommendedDishesExample dishesExample = new RecommendedDishesExample();
        dishesExample.createCriteria().andShopIdEqualTo(shop.getShopId());
        List<RecommendedDishes> dishesList = dishesMapper.selectByExample(dishesExample);
        System.out.println("service dishesList :" + dishesList);
        vo.setShop(shop);
        vo.setRecommendedDishesList(dishesList);
        return ServerResult.getSuccess(vo);

    }
}
